package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.exception.BssException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 缴费起止日期校验YYYY-MM
 */
@Service
public class CheckJfStartEndDateAttrHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void init(CommonAttrVO commonAttrVO) {

    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {

        if (KiteStringUtils.isEqual("USR0014", fieldName)) { //缴费周期开始时间
            KiteAttr jfEndDateAttr = querySceneInstService
                .getCommonAttrsByFieldName(sceneInstId, "USR0015", "113");//缴费周期截止时间
            if (KiteStringUtils.isNotEmpty(value) && KiteStringUtils.isNotEmpty(jfEndDateAttr.getValue())) {
                Date startDate = simpleHtDate(value);
                Date endDate = simpleHtDate(jfEndDateAttr.getValue());
                if(null!=startDate && null!=endDate) {
                    if (startDate != null && startDate.after(endDate)) {
                        BssException.throwOut(KeyConsts.LOG_MODULE, "", "缴费周期开始时间必须小于缴费周期截止时间");
                    }
                }
            }
        }else if (KiteStringUtils.isEqual("USR0015", fieldName)) { //缴费周期截止时间
            KiteAttr jfStartDateAttr = querySceneInstService
                .getCommonAttrsByFieldName(sceneInstId, "USR0014", "113");//缴费周期开始时间
            if (KiteStringUtils.isNotEmpty(value) && KiteStringUtils.isNotEmpty(jfStartDateAttr.getValue())) {
                Date startDate = simpleHtDate(jfStartDateAttr.getValue());
                Date endDate = simpleHtDate(value);
                if(null!=startDate && null!=endDate) {
                    if (startDate.after(endDate)) {
                        BssException.throwOut(KeyConsts.LOG_MODULE, "", "缴费周期截止时间必须大于缴费周期开始时间");
                    }
                }
            }
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }

    private Date simpleHtDate(String dateV){
        Date dateD = null;
        if(dateV.contains("-")){
            SimpleDateFormat dateFormator = new SimpleDateFormat("yyyy-MM-dd");
            dateD = dateFormator.parse(dateV.concat("-01"), new ParsePosition(0));
        }else {
            SimpleDateFormat dateFormator = new SimpleDateFormat("yyyyMMdd");
            dateD = dateFormator.parse(dateV, new ParsePosition(0));
        }
        return dateD;
    }

}
